package com.example.administrator.pregnancyassistence.fragment;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ListView;

import com.example.administrator.pregnancyassistence.R;
import com.example.administrator.pregnancyassistence.adapter.NewsAdapter;
import com.example.administrator.pregnancyassistence.bean.NewsBean;
import com.example.administrator.pregnancyassistence.util.AssetsUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

public class NewsListFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news_list, container, false);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getActivity().getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
        view.setFitsSystemWindows(true);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        String newsJson = AssetsUtil.getNewsJson(getActivity(),"news_list.json");

        Gson gson = new Gson();
        List<NewsBean> newsBeanList = gson.fromJson(newsJson, new TypeToken<List<NewsBean>>(){}.getType());

//            for (int i = 0; i < jsonArray.length(); i++) {
//                Object newsItem = jsonArray.get(i);
//                if (newsItem instanceof JSONObject) {
//                    JSONObject newsItemJsonObject = (JSONObject) newsItem;
//                    NewsBean newsBean = new NewsBean();
//                    newsBean.setContent(newsItemJsonObject.getString("content"));
//                    newsBean.setName(newsItemJsonObject.getString("name"));
//                    newsBean.setNewsImage(newsItemJsonObject.getString("newsImage"));
//                    JSONArray tagList = newsItemJsonObject.getJSONArray("tagList");
//                    List<String> tagsList = new ArrayList<>();
////                    for (int i = 0; i<)
//                    newsBeanList.add(newsBean);
//                }
//            }

        ListView newsListView = getView().findViewById(R.id.lv_news_list);
        NewsAdapter newsAdapter = new NewsAdapter(getContext(), newsBeanList);
        newsListView.setAdapter(newsAdapter);
    }

}
